package wnhz.com.business.shareshop.activity.mine;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.rebateshop.activity.home.ShareFriandActivity;
import wnhz.com.business.shareshop.bean.YaJinDetailsBean;
import wnhz.com.business.shareshop.bean.YajinBean;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * 共享商城我的押金详情页面
 */
public class DepositDetailsActivity extends MyBaseActivity implements ActionBarClickListener {
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.rv_deposit)
    RecyclerView rvDeposit;//押金商品
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_color)
    TextView tv_color;
    @BindView(R.id.tv_amount)
    TextView tv_amount;
    @BindView(R.id.tv_newprice)
    TextView tv_newprice;
    @BindView(R.id.add)
    ImageView add;
    @BindView(R.id.num)
    TextView num;

    private String id,statuse;
    private YajinBean.InfoBean.ListBean item=null;
    private List<YaJinDetailsBean.InfoBean.UserListBean> data = new ArrayList<>();

    @Override
    protected void handleMsg(Message msg) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deposit_details);
        ButterKnife.bind(this);
        if(null!=getIntent()){
            id = getIntent().getStringExtra("id");
            statuse = getIntent().getStringExtra("statuse");
            item = (YajinBean.InfoBean.ListBean)getIntent().getSerializableExtra("data");
            setTop();
        }
        String title="";
        if(statuse.equals("0")){
            title="退款中";
        }else if(statuse.equals("1")){
            title="返利中";
        }
        actionbar.setData(title, R.drawable.ic_lefft_white, null, 0, "", 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        loadData();
    }
    private void setTop() {//设置头部数据
        Glide.with(DepositDetailsActivity.this).load(item.getGoods_pic()).into(img);
        tv_name.setText(item.getGoods_name());
        tv_color.setText(item.getSku_info());
        tv_amount.setText("  ×"+item.getGoods_nums());
        tv_newprice.setText(item.getGoods_price());
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转分享页面  (页面标题: 分享商品给好友)
                startActivity(ShareFriandActivity.createIntent(DepositDetailsActivity.this,item.getGoods_pic(),"",item.getGoods_name(),item.getGoods_price()));
            }
        });
    }
    private void loadData() {
        Map<String, Object> map = new HashMap<>();
        /**
         * 我的押金详情
         * @param str   token
         * @param int   order_goods_id
         */
        map.put("token", MyApplication.getToken());
        map.put("order_goods_id", id);
        XUtil.Post(Url.UCENTER_MYDEPOSIT_DETAILS, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("re");
                    if (re.equals("1")) {
                        Gson gson = new Gson();
                        YaJinDetailsBean shoppingCartListBean = gson.fromJson(result, YaJinDetailsBean.class);
                        data = shoppingCartListBean.getInfo().getUser_list();
                        num.setText("×"+shoppingCartListBean.getInfo().getCount());
                        initdatabuy();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }
    private void initdatabuy() {
        rvDeposit.setLayoutManager(new LinearLayoutManager(getApplication(), LinearLayoutManager.VERTICAL, false));
        rvDeposit.setAdapter(new BaseRVAdapter(getApplication(), data) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_deposit_details;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                Glide.with(DepositDetailsActivity.this).load(data.get(position).getHead_img()).into(holder.getImageView(R.id.img));
                holder.getTextView(R.id.tv_name).setText(data.get(position).getUsername());
                holder.getTextView(R.id.tv_color).setText(data.get(position).getTime());
            }
        });
    }


    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }
}
